package it.polimi.swim.servlet;

import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.SkillManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class EditProfileServlet
 */
public class EditProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Context ctx;

	public EditProfileServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			int id;
			SystemUser user;

			// Inizializzo contesto
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			// Dati sessione (recupero id)
			HttpSession session = request.getSession();
			id = (Integer) session.getAttribute("id");

			// Richiamo il session bean
			ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			Object ref2 = ctx.lookup("SkillManagerBean/remote");
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref2;
			List<Skill> skillList = skillManager.availableSkills();

			request.setAttribute("skillList", skillList);

			// Richiamo searchUserById del session
			user = systemUserManager.searchUserById(id);
			
			request.setAttribute("user", user);

			request.getRequestDispatcher("/editProfile.jsp").forward(request,
					response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String psw, name, surname, city;
			int id;
			// inizializzo contesto
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);
			HttpSession session = request.getSession();
			// recupero parametri

			psw = request.getParameter("psw");
			name = request.getParameter("name");
			surname = request.getParameter("surname");
			city = request.getParameter("city");
			id =(Integer) session.getAttribute("id");
			// TODO date

			// Richiamo il session bean
			ctx = new InitialContext();
			Object ref = ctx.lookup("SystemUserManagerBean/remote");
			SystemUserManagerBeanRemote systemUserManager = (SystemUserManagerBeanRemote) ref;
			session.setAttribute("name", name);
			session.setAttribute("surname", surname);
				
			systemUserManager.editOldUser(id, name, surname, psw, city);
			request.getRequestDispatcher("/homeUser.jsp").forward(request,
					response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
